package com.runfast.waimai.web.controller;

import com.runfast.common.utils.TokenUtil;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.common.dao.model.RunfastAgentbusiness;
import com.runfast.common.service.RunfastAgentbusinessService;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.*;
import com.runfast.waimai.web.dto.BusinessDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 外卖订单接口
 */
@RestController("userWmHomeController")
@RequestMapping(value= "/api/user/wm/home", headers = "token")
public class HomeController{


    @Autowired
    private BsTravelAdvertService advertService;


    @Autowired
    private RunfastAgentbusinessService agentbusinessService;

    @Autowired
    private RunfastBusinessService businessService;

    @Autowired
    private RunfastHomepageService homepageService;


    @Autowired
    private RunfastGoodsSellService goodsSellService;


    @Autowired
    private RunfastActivityService activityService;
    /**
     *
     * 根据用户当前的位置获取默认的代理商
     * @param userLng 用户当前的经度
     * @param userLat 用户当前的纬度
     * @param token
     * @return
     */
    @PostMapping("/agent")
    public Result agent(@RequestParam double userLng, @RequestParam double userLat, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);

        RunfastAgentbusiness agentNearBy = agentbusinessService.getAgentNearBy(userLng, userLat);

        if(agentNearBy!=null)return Result.ok("", agentNearBy);
        else return  Result.fail(ResultCode.FAIL, "附近不存在代理商");

    }

    /**
     *
     * 获取附近商家
     * @param userLng 用户当前经度
     * @param userLat 用户当前维度
     * @param sorting 排序方式
     * @param activityType 活动类型
     * @param catalogId 商户分类id
     * @param pageable
     * @param token
     * @return
     */
    @PostMapping("/businessNearBy")
    public Result<List<Map<String, Object>>> businessNearBy(@RequestParam Integer agentId,@RequestParam double userLng, @RequestParam double userLat, @RequestParam Integer sorting, @RequestParam(required = false) Integer activityType, @RequestParam(required = false) Integer catalogId, @PageableDefault Pageable pageable, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);

        List<Map<String, Object>> maps = businessService.nearBy(agentId, null, userLng, userLat, sorting, activityType, catalogId,pageable);

//        List<BusinessDto> businessDtos =  businessService.nearBy(longitude,latitude, sorting, filter, catalogId);

        return Result.ok("",maps);
    }
    /**
     * 获取代理商促销信息
     * @param agentId 代理商id
     * @param token
     * @return
     */
    @PostMapping("/page")
    public Result<Map<String, Object>> page(@RequestParam int agentId,  @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);

        /**
         * 根据代理商id获取首页轮播广告列表
         */
        BsTravelAdvertExample advertExample = new BsTravelAdvertExample();
        advertExample.or().andLocationEqualTo(1).andUsedEqualTo(1).andAgentIdEqualTo(agentId).andTypeEqualTo(0);

        List<BsTravelAdvert> advertList = advertService.selectByExample(advertExample);


        /**
         * 根据代理商id获取首页商户分类
         */
        RunfastHomepageExample homepageExample = new RunfastHomepageExample();
        homepageExample.or().andAgentIdEqualTo(agentId);
        List<RunfastHomepage> homepageList = homepageService.selectByExampleWithPageable(homepageExample, PageRequest.of(0, 10));

        /**
         *获取代理商优惠专区大额满减或者免配送费活动
         */
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(1);//满减
        activityTypeList.add(5);//免全部
        activityTypeList.add(7);//免全部
        List<RunfastActivity> agentZoneBusiness = activityService.getAgentZoneActivityIn(agentId, activityTypeList);

        /**
         * 获取特惠优选商品列表
         */
        List<Map<String, Object>> offZoneGoods = goodsSellService.findAgentOffZoneGoods(agentId, PageRequest.of(0,8));

        Map<String,Object> page = new HashMap<>();
        page.put("advertList",advertList);
        page.put("homepageList",homepageList);
        page.put("agentZoneBusiness",agentZoneBusiness);
        page.put("offZoneGoods",offZoneGoods);
        return Result.ok("",page);
    }



    /**
     *
     * 根据名称搜索商家或者商品
     * @param userLng 用户当前经度
     * @param userLat 用户当前纬度
     * @param name 商家名或者商品名
     * @param agentId 代理商id
     * @param pageable
     * @param token
     * @return
     */
    @PostMapping("/search")
    public Result<List<Map<String, Object>>> search(@RequestParam int agentId, @RequestParam String name, @RequestParam Double userLng, @RequestParam Double userLat, @PageableDefault Pageable pageable, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);

        List<Map<String, Object>> search = businessService.search(name, agentId, userLng, userLat, pageable);

        return Result.ok("",search);
    }


    /**
     *
     * 获取代理商优惠专区大额满减或者免配送费活动商家列表
     * @param activityId 活动id
     * @param pageable
     * @param token
     * @return
     */
    @PostMapping("/zoneBusiness")
    public Result<List<BusinessDto>> zoneBusiness(@RequestParam Integer activityId, @RequestParam Double userLng, @RequestParam Double userLat, @PageableDefault Pageable pageable, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);


        List<BusinessDto> agentZoneBusiness = businessService.getAgentZoneBusiness(activityId, userLng, userLat, pageable);
        return Result.ok("", agentZoneBusiness);
    }


    /**
     * 获取特惠优选更多商品列表
     * @param agentId
     * @param pageable
     * @param token
     * @return
     */
    @PostMapping("/offZoneMore")
    public Result<List<Map<String, Object>>> offZoneMore(@RequestParam int agentId, @PageableDefault Pageable pageable, @RequestHeader String token) {
        Integer userId = TokenUtil.getUserId(token);

        List<Map<String, Object>> offZoneGoods = goodsSellService.findAgentOffZoneGoods(agentId, pageable);

        return Result.ok("",offZoneGoods);
    }








}
